package com.inteligencia.stage.web.usuario;

import com.inteligencia.stage.business.dto.EventoDTO;
import com.inteligencia.stage.business.dto.LoginDTO;
import com.inteligencia.stage.business.dto.PersonaDTO;
import com.inteligencia.stage.business.dto.TareaDTO;
import com.inteligencia.stage.dataaccess.domain.PersonaDAO;
import com.inteligencia.stage.dataaccess.domain.UsuarioDAO;
import com.inteligencia.stage.dataaccess.model.TareaVO;
import com.inteligencia.stage.dataaccess.model.UsuarioVO;
import java.util.Set;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@ParentPackage(value = "stage")
public class GestionUsuarios extends ActionSupport {

    private static final long serialVersionUID = -7133848243722723891L;
    private String theme = "showcase";
    private Map<String, String> themes;
    private LoginDTO login;
    private ApplicationContext ctx;
    private PersonaDAO personaDAO;
    private HttpSession session;
    private List<EventoDTO> eventos;

    @Action(value = "/ingresar", results = {
        @Result(location = "index.jsp", name = "error"),
        @Result(location = "usuario/eventos.jsp", name = "success")
    })
    @Override
    public String execute() {
        session = ServletActionContext.getRequest().getSession();

        ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        personaDAO = (PersonaDAO) ctx.getBean("personaDAO");
        //usuarioVO = 

        PersonaDTO personaDTO = new PersonaDTO(personaDAO.getByLogin(login));

        if (personaDTO == null) {
            return ERROR;
        } else {
            session.setAttribute("usuario", personaDTO);
        }

        return SUCCESS;
    }

    @Action(value = "/salir", results = {
        @Result(location = "index.jsp", name = "success")
    })
    public String salir() {
        System.out.println("Accion salir");
        session = ServletActionContext.getRequest().getSession();
        session.setAttribute("usuario", null);
        return SUCCESS;
    }

    public LoginDTO getLogin() {
        return login;
    }

    public void setLogin(LoginDTO login) {
        this.login = login;
    }

    public List<EventoDTO> getEventos() {
        return eventos;
    }

    public void setEventos(List<EventoDTO> eventos) {
        this.eventos = eventos;
    }

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    public Map<String, String> getThemes() {
        return themes;
    }

    public void setThemes(Map<String, String> themes) {
        this.themes = themes;
    }
}
